export default {
	data() {
		return {
			//数据
      validate: {} //验证对象，
		}
	},
created() { //调用方法
    let that = this;
    //获取当前cas返回参数信息
    let data = this.$route.query.data;
    let token = this.$route.query.token;
    //cas登录成功后返回的信息
    if (data != null && data != ""){
      this.initLoginUser(JSON.parse(data));
    }
    //获取当前cas返回token信息
    if (token != null && token != "") {
      that.$post(
        that.urls.getUserRoleByToken,
        {"token": token}
      )
      .then(function (res) {
        if (res.success) {
          that.initLoginUser(res.data);
        }
      })
    }
	},
	mounted() {
		// 内容版块获取浏览器尺寸
		var autoDivHeight = document.getElementById('Login');
		var deviceWidth = document.documentElement.clientWidth;
		var deviceHeight = document.documentElement.clientHeight;
		autoDivHeight.style.width = (Number(deviceWidth) -0) + 'px'; //数字是页面布局宽度差值
		autoDivHeight.style.height = (Number(deviceHeight) - 120) + 'px'; //数字是页面布局高度差

    let that = this;
    //加载表单验证
    that.validate = $("#bindingCasAccountForm").validate({
      ignore: [],
      rules: {
        userAccount: { // 登录用户名
          required: true,
          rangelength: [15,18]
        },
        userPassword: { // 登录密码
          required: true,
          rangelength: [6,16]
        }
      },
      messages: {
        userAccount: { // 登录用户名
          required: "请输入用户名",
          rangelength: "用户名只能输入18个字！"
        },
        userPassword: { // 登录密码
          required: "请输入密码",
          rangelength: "密码只能输入6-16个字！"
        }
      },
    });
	},
	methods: { //写函数方法
    //绑定用户
    bindingCasAccount: function () {
      let that = this;
      //let ticket = this.$route.query.ticket;
      let name = this.$route.query.name;
      let flg = that.validate.form();
      if (flg && name != null) {
        //ticket = decodeURIComponent(ticket); //解析url特殊字符
        let dataObj = that.combinationFormObject(that.validate.currentForm);
        dataObj["casAccount"] = name;
        that.$post(
          that.urls.bindingCasAccount,
          dataObj
        )
          .then(function (res) {
            let msg = "";
            if (res.success) {
              msg = "绑定成功！";
              that.initLoginUser(res.data);
            } else {
              msg = "绑定失败！";
            }
            layer.msg(msg, {
              time: 3000, //3s后自动关闭
            });
          })
      }
    },
    //初始化登录用户
    initLoginUser: function (data) {
      if (data == null || data == "") {
        return;
      }
      if(data.userInfo.userAccountStatus == 0){
        layer.msg('账号被冻结！', {
          time: 3000, //3s后自动关闭
        });
        return;
      }
      if(data.role.roleStatus == 0){
        layer.msg('账号角色被冻结！', {
          time: 3000, //3s后自动关闭
        });
        return;
      }
      //将用户信息存入sessionStorage
      this.set("token", data.token);//token
      this.set("userRole", data.role);//角色信息
      this.set("userInfo", data.userInfo);//用户信息
      //跳转到主页
      this.$router.push({
        path: "/index",
      })
    }
	}
}
